/*
 * @(#)MySqlODPEntryIterator.java	0.1 02/04/06
 *
 * Copyright 2006 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.se.odp;

import java.util.Iterator;
import java.util.logging.Logger;

/**
 *
 */
public class MySqlODPEntryIterator implements ODPEntryIterator {
  protected int[] ids;
  protected MySqlODPStorer storer;
  protected int currentEntry = 0;
  protected Logger logger = Logger.getLogger(MySqlODPEntryIterator.class.getName());

  MySqlODPEntryIterator(MySqlODPStorer storer, int[] ids) {
    this.storer = storer;
    this.ids = ids;
  }
  
  public int size() {
	  return ids.length;
  }

  /**
   * Returns true if the iteration has more entries.
   */
  public boolean hasNext() {
    return currentEntry < ids.length;
  }

  /**
   * Returns the next complete entry in the iteration.
   */
  public Object next() {
    ODPEntry e = new ODPEntry();
    if (currentEntry < ids.length) {
      try {
        e.id = (new Integer(ids[currentEntry])).toString();
        e = storer.get(e);
      } catch (Exception ex) {
        logger.severe(ex.toString());
      }
      currentEntry++;
    }
    return e;
  }

  /**
   * Removes from the underlying collection the last entry returned by the
   * iterator.
   */
  public void remove() {
    if ((currentEntry > 0) && (currentEntry <= ids.length)) {
      try {
        storer.remove((new Integer(ids[currentEntry])).toString());
      } catch (Exception ex) {
        logger.severe(ex.toString());
      }
    }
  }
}
